package hwaniiiii.dodol;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener{
	
	
	
    private SharedPreferences mPrefs;
	private WorldCupApp mApp;

	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        mApp = (WorldCupApp) getApplication();
        
        ((View)findViewById(R.id.countries)).setOnClickListener(this);
        ((View)findViewById(R.id.schedule_and_result)).setOnClickListener(this);
        
        mPrefs = getSharedPreferences("preference", Context.MODE_PRIVATE);
        if(!mPrefs.contains("unique_key")) {
        	Editor edit = mPrefs.edit();
        	String id = String.valueOf(System.currentTimeMillis() * Math.random());
        	edit.putString("unique_key", id);
        	edit.commit();
        }
        if(!mApp.updateData()) finish();
    }
	
	@Override
	protected void onResume() {
		super.onResume();
		TextView tv = (TextView)findViewById(R.id.text_bar);
		tv.setText(mApp.getTextBarString());

	}
	

	public void onClick(View v) { 
		switch(v.getId()) {
			case R.id.countries:
				startActivity(new Intent(this, CountriesActivity.class));
				break;
			case R.id.schedule_and_result:
				startActivity(new Intent(this, ScheduleAndResultActivity.class));
				break;
		}
	}


	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	mApp.getAddMenu(menu);
        return true;
    }

	@Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	mApp.getAboutDialog(this).show();
    	return true;
    }
}